//
// Created by Tan Ke on 2024/1/22.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

enum GridVisitStat
{
    NO_VISITED = 0,
    VISITED,
};

bool checkValidGrid(int** grid, int gridSize, int* gridColSize)
{
    int visitCnt = 0;
    int cur_row = 0;
    int cur_col = 0;

    if (grid[0][0] != 0) {
        return false;
    }

    visitCnt += 1;

    while (visitCnt < gridSize * gridSize) {
        int cur_val = grid[cur_row][cur_col];
        int next_val = cur_val + 1;

        /* 尝试八种走法 */
        if (cur_row > 0 && cur_col > 1) {
            if (grid[cur_row-1][cur_col-2] == next_val) {
                visitCnt += 1;
                cur_row = cur_row - 1;
                cur_col = cur_col - 2;
                continue;
            }
        }
        if (cur_row > 1 && cur_col > 0) {
            if (grid[cur_row-2][cur_col-1] == next_val) {
                visitCnt += 1;
                cur_row = cur_row - 2;
                cur_col = cur_col - 1;
                continue;
            }
        }
        if (cur_row > 1 && cur_col < gridSize - 1) {
            if (grid[cur_row-2][cur_col+1] == next_val) {
                visitCnt += 1;
                cur_row = cur_row - 2;
                cur_col = cur_col + 1;
                continue;
            }
        }
        if (cur_row > 0 && cur_col < gridSize - 2) {
            if (grid[cur_row-1][cur_col+2] == next_val) {
                visitCnt += 1;
                cur_row = cur_row - 1;
                cur_col = cur_col + 2;
                continue;
            }
        }
        if (cur_row < gridSize -1 && cur_col > 1) {
            if (grid[cur_row+1][cur_col-2] == next_val) {
                visitCnt += 1;
                cur_row = cur_row + 1;
                cur_col = cur_col - 2;
                continue;
            }
        }
        if (cur_row < gridSize - 2 && cur_col > 0) {
            if (grid[cur_row+2][cur_col-1] == next_val) {
                visitCnt += 1;
                cur_row = cur_col + 2;
                cur_col = cur_col - 1;
                continue;
            }
        }
        if (cur_row < gridSize - 2 && cur_col < gridSize - 1) {
            if (grid[cur_row+2][cur_col+1] == next_val) {
                visitCnt += 1;
                cur_row = cur_row + 2;
                cur_col = cur_col + 1;
                continue;
            }
        }
        if (cur_row < gridSize - 1 && cur_col < gridSize - 2) {
            if (grid[cur_row+1][cur_col+2] == next_val) {
                visitCnt += 1;
                cur_row = cur_row + 1;
                cur_col = cur_col + 2;
                continue;
            }
        }
        break;
    }

    if (visitCnt == gridSize * gridSize) {
        return true;
    }
    return false;
}

int main(void)
{
    return 0;
}